package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.entity.Book;
import work.entity.Bookware;
import work.service.BookService;
import work.service.BookwareService;
import work.vo.DataVo;

@RestController
@RequestMapping("/book")
public class BookHandler {

    @Autowired
    private BookwareService bookwareService;
    @Autowired
    private BookService bookService;

    @GetMapping("/test")
    public DataVo test() {
        return new DataVo();
    }

    // 工作人员删除图书
    // 前端传入一个book对象
    @PostMapping("/deleteBook")
    public DataVo deleteBook(@RequestBody Book book) {
        DataVo dataVo = new DataVo();

        if (bookService.updatebookStatus2(book) == 0) {
            dataVo.setCode(240);
            dataVo.setMsg("更新失败");
            return dataVo;
        }

        Bookware bookware = bookwareService.findById(book.getBookWareId());
        bookware.setBookTotal(bookware.getBookTotal() - 1);
        if (bookwareService.updateWare(bookware) == 0 ) {
            dataVo.setCode(241);
            dataVo.setMsg("更新失败");
        } else {
            dataVo.setCode(242);
            dataVo.setMsg("更新成功");
        }
        return dataVo;
    }

    // 工作人员修改图书信息（修改状态为“不可借”）
    // 前端传入一个book对象
    @PostMapping("/changeBook")
    public  DataVo changeBook(@RequestBody Book book) {
        DataVo dataVo = new DataVo();

        if (bookService.updatebookStatus3(book) == 0) {
            dataVo.setCode(250);
            dataVo.setMsg("修改失败");
        } else {
            dataVo.setCode(251);
            dataVo.setMsg("修改成功");
        }
        return dataVo;
    }
}